package com.yangye.pisystem.controller;

import com.yangye.picommon.model.PiResult;
import com.yangye.picommon.model.page.PageRequest;
import com.yangye.picommon.model.page.PageResult;
import com.yangye.pisystem.domain.SysUser;
import com.yangye.pisystem.dto.SysUserDTO;
import com.yangye.pisystem.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * Created by yangye on 2019/7/9
 */
@RestController
@RequestMapping("/sys/users")
public class SysUserController {

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 新增
	 * @param sysUser
	 * @return
	 */
	@PostMapping("/")
	@PreAuthorize("hasAuthority('sys:user:add')")
	PiResult add(@RequestBody SysUserDTO sysUser) {
		sysUserService.addUser(sysUser);
		return PiResult.succeed();
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	@PreAuthorize("hasAuthority('sys:user:remove')")
	PiResult remove(@PathVariable Long id) {
		sysUserService.remove(id);
		return PiResult.succeed();
	}

	/**
	 * 修改
	 * @param sysUser
	 * @return
	 */
	@PutMapping("/")
	@PreAuthorize("hasAuthority('sys:user:update')")
	PiResult update(@RequestBody SysUserDTO sysUser) {
		sysUserService.updateUser(sysUser);
		return PiResult.succeed();
	}

	/**
	 * 查看详情
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	@PreAuthorize("hasAuthority('sys:user:view')")
	PiResult get(@PathVariable Long id) {
		SysUser sysUser = sysUserService.get(id);
		return PiResult.succeed(sysUser);
	}

	/**
	 * 查看分页
	 * @param sysUser
	 * @param pageRequest
	 * @return
	 */
	@GetMapping("/page")
	@PreAuthorize("hasAuthority('sys:user')")
	PiResult listPage(SysUser sysUser, PageRequest pageRequest) {
		PageResult<SysUser> page = sysUserService.findPage(sysUser, pageRequest);
		return PiResult.succeed(page);
	}
}
